package client.ui;

import java.util.List;

import phasebook.client.msgs.UserProfileInfo;

import client.Controller;
import client.Controller.ResponseHandler;
import client.api.CombinedResponse;

import com.smartgwt.client.types.HeaderControls;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.HeaderControl;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FloatItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.IntegerItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.LayoutSpacer;

public class EditProfile extends Window implements ChangedHandler, ResponseHandler {
		private TextItem nameItem = new TextItem();
		private TextItem usernameItem = new TextItem();
		private TextItem emailItem = new TextItem();
		private IntegerItem bankAccountIdItem = new IntegerItem();
		
		private PasswordItem passwordItem = new PasswordItem();
		private PasswordItem password2Item = new PasswordItem();
		
		private FloatItem balanceItem = new FloatItem();
		
		private Button ok = new Button("Update");

		private UserProfileInfo userProfile = new UserProfileInfo();

		public EditProfile(final Controller controller, final UserProfileInfo userProf){
			this.userProfile = userProf;
			HeaderControl close = new HeaderControl(HeaderControl.CLOSE,
					new ClickHandler() {
						@Override
						public void onClick(ClickEvent event) {
							hide();
						}
					});
			setHeaderControls(HeaderControls.HEADER_LABEL, close);
			setWidth(260);
			setHeight(240);
			setTitle("Update user profile");
			setShowMinimizeButton(false);
			setIsModal(true);
			setShowModalMask(true);
			centerInPage();
	
			final DynamicForm form = new DynamicForm();
			form.setWidth100();
	
			nameItem.setValue("");
			nameItem.setTitle("Name");
			nameItem.setRequired(true);
			nameItem.addChangedHandler(this);
			nameItem.setValue(userProfile.getName());
	
			usernameItem.setValue("");
			usernameItem.setTitle("Username");
			usernameItem.setRequired(true);
			usernameItem.addChangedHandler(this);
			usernameItem.setValue(userProfile.getUsername());
	
			emailItem.setValue("");
			emailItem.setTitle("Email");
			emailItem.setRequired(true);
			emailItem.addChangedHandler(this);
			emailItem.setValue(userProfile.getEmail());
	
			bankAccountIdItem.setValue("");
			bankAccountIdItem.setTitle("Bank Account Id");
			bankAccountIdItem.setRequired(true);
			bankAccountIdItem.addChangedHandler(this);
			bankAccountIdItem.setValue(userProfile.getBankAccountId());
			
			
			passwordItem.setValue("");
			passwordItem.setTitle("Password");
			passwordItem.setRequired(true);
			passwordItem.addChangedHandler(this);
			passwordItem.setValue(userProfile.getPassword());
			
			password2Item.setValue("");
			password2Item.setTitle("Password again");
			password2Item.setRequired(true);
			password2Item.addChangedHandler(this);
			password2Item.setValue(userProfile.getPassword());
			
			balanceItem.setValue("");
			balanceItem.setTitle("Balance");
			balanceItem.setRequired(true);
			balanceItem.addChangedHandler(this);
			balanceItem.setValue(userProfile.getBalance());
			
				
			form.setFields(new FormItem[] { nameItem, usernameItem, emailItem, bankAccountIdItem,
					passwordItem, password2Item, balanceItem });
			
			ok.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					UserProfileInfo req = new UserProfileInfo();
					req.setUserId(userProfile.getUserId());
					req.setName(nameItem.getValueAsString());
					req.setEmail(emailItem.getValueAsString());
					req.setBankAccountId(bankAccountIdItem.getValueAsInteger());
					req.setUsername(usernameItem.getValueAsString());
					req.setPassword(passwordItem.getValueAsString());
					
					controller.updateUser(req);
					double balanceDif = balanceItem.getValueAsFloat() - userProfile.getBalance();
					if (balanceDif!=0)
						controller.updateBalance(balanceDif, userProfile.getBankAccountId());  
					
					ok.disable();
					hide();
				}
			});
			
			ok.disable();
			
			HLayout layout = new HLayout();
			layout.addMember(new LayoutSpacer());
			layout.addMember(ok);
	
			addItem(form);
			addItem(layout);
			
			controller.addResponseHandler(this);
		}

		@Override
		public void onChanged(ChangedEvent event) {
			if (nameItem.getValueAsString().length()==0||
				usernameItem.getValueAsString().length()==0||
				emailItem.getValueAsString().length()==0||
				passwordItem.getValueAsString().length()==0||
				!passwordItem.getValueAsString().equals(password2Item.getValueAsString()))
				ok.disable();
			else
				ok.enable();
		}

		@Override
		public void onResponse(CombinedResponse response) {
			List<UserProfileInfo> profiles = response.getProfiles();
			if (profiles != null)
				for (UserProfileInfo p : profiles)
					if (p.getUserId() == userProfile.getUserId())
						userProfile = p;
		}

}
